import {observable, action} from "mobx";
import {ListViewPage, SearchAdvanced} from "weaver-mobile-page";
import * as API from "../../api";

export default class ProjectsStore {
    @observable nowTab = 0;//当前tab:0左侧 1右侧
    @observable searchValue = "";
    @observable searchValue2 = "";
    @observable chooseIndex = -1;
    @observable chooseIndex2 = -1;
    @observable conditionReady = false;
    @observable conditionReady2 = false;
    @observable tabkey = {
        "0": "doing",
        "1": "frozen",
        "2": "complete",
    }
    @observable conditiontype = {
        "0": "mymanagerprj",
        "1": "search"
    }
    @observable listAd = new ListViewPage.Store({route: "/index/projects?tabs=0", id: "0"});
    @observable listAdMine = new ListViewPage.Store({route: "/index/projects?tabs=1", id: "1"});

    @observable searchAd = new SearchAdvanced.Store({route: "/index/projects", id: "0"});
    @observable searchAd2 = new SearchAdvanced.Store({route: "/index/projects", id: "1"});
    @observable conditionFetchOptions = {
        url: "/api/prj/moblie/project/getPrjSearchCondition",
        method: "GET",
        params: {conditiontype: "mymanagerprj"}
    }
    @observable conditionFetchOptions2 = {
        url: "/api/prj/moblie/project/getPrjSearchCondition",
        method: "GET",
        params: {conditiontype: "search"}
    }

    @action
    init = (index, fresh) => {
        this.nowTab = Number(index);
        this.initSearchAd(fresh, this.onSearch)
    }
    @action
    initSearchAd = (fresh, callback) => {
        if (fresh === undefined || fresh === "yes") {
            this.searchAd.form.resetFields()
            this.searchAd.getData(this.conditionFetchOptions).then((form) => {
                form.updateField("prjname", this.searchValue);
                if (this.nowTab === 0) {
                    callback();
                }
                this.judgeIconShow();
            });
            this.searchAd2.form.resetFields()
            this.searchAd2.getData(this.conditionFetchOptions2).then((form) => {
                form.updateField("prjname", this.searchValue2);
                if (this.nowTab === 1) {
                    callback();
                }
                this.judgeIconShow();
            });
        } else {
            this.onSearch()
            this.judgeIconShow();
        }
    }

    @action
    onTabChange = (index) => {
        this.nowTab = Number(index);
        this.onSearch();
    };
    @action
    changeSearchValue = (val = "") => {
        if (this.nowTab === 0) {
            this.searchValue = val;
            this.searchAd.form.updateField("prjname", val);
        } else {
            this.searchValue2 = val;
            this.searchAd2.form.updateField("prjname", val);
        }
        this.judgeIconShow();
    };
    @action
    onReset = () => {
        this.changeSearchValue();
    }

    @action
    onSearchAdvancedChange = (data, value) => {
        if (this.nowTab === 0) {
            const {formParams} = this.searchAd.form
            this.searchValue = formParams["prjname"]
        } else {
            const {formParams} = this.searchAd2.form
            this.searchValue2 = formParams["prjname"]
        }
        this.judgeIconShow();
    }
    @action
    judgeIconShow = () => {
        if (this.nowTab === 0) {
            const {formParams} = this.searchAd.form
            this.conditionReady = false;
            Object.keys(formParams).forEach(v => {
                if (formParams[v]) {
                    this.conditionReady = true;
                }
            })
        } else {
            const {formParams} = this.searchAd2.form
            this.conditionReady2 = false;
            Object.keys(formParams).forEach(v => {
                if (formParams[v]) {
                    this.conditionReady2 = true;
                }
            })
        }
    }

    @action
    onSearch = () => {
        let list, params;
        if (this.nowTab === 0) {
            params = {"conditiontype": "mymanagerprj", pageindex: 1};
            list = this.listAd;
            if (this.chooseIndex !== -1) {
                params = {
                    ...params,
                    tabkey: this.tabkey[this.chooseIndex]
                }
            }
            Object.assign(params, this.searchAd.form.formParams);
        } else {
            params = {"conditiontype": "search", pageindex: 1};
            list = this.listAdMine;
            if (this.chooseIndex2 !== -1) {
                params = {
                    ...params,
                    tabkey: this.tabkey[this.chooseIndex2]
                }
            }
            Object.assign(params, this.searchAd2.form.formParams);
        }

        // 清除列表数据
        list.clearData();
        // 带参数请求列表数据
        this.getProjectList(list, params)
    };
    @action
    getProjectList = (list, params) => {
        API.getProjectList(params).then(action(res => {
            list.getData({dataKey: res.datas});
        }))
    };

    @action
    chooseState = (opt) => {
        if (this.nowTab === 0) {
            if (this.chooseIndex === opt.index) {
                this.chooseIndex = -1;
            } else {
                this.chooseIndex = opt.index;
            }
        } else {
            if (this.chooseIndex2 === opt.index) {
                this.chooseIndex2 = -1;
            } else {
                this.chooseIndex2 = opt.index;
            }
        }
        this.onSearch();    //带参请求数据
    };

}
